package gameheart.client.logic;

import gameheart.client.sprite.CardSprite;
import java.awt.Graphics;
import java.awt.Point;

public class DealingState extends PlayState {

    private CardSprite _cards[];
    private CardSprite _dealingCard;
    private CardSprite _players[][] = new CardSprite[4][13];
    private int _remainCardNumber;
    private int _currentPlayerIndex;
    private int[] _cardOfPlayer = new int[4];

    public DealingState(PlayCanvas context) {
        super(context);
        //_context.updateMark();
        _remainCardNumber = 52;
        _cards = new CardSprite[_remainCardNumber];
        for (int index = 0; index < _cards.length; ++index) {
            _cards[index] = new CardSprite((int) _context._centerPoint.getX(), (int) _context._centerPoint.getY(), null, _context._imageResposity);
        }
        _dealingCard = null;
        _currentPlayerIndex = 0;


    }

    @Override
    public PlayState Clone() {
        return new DealingState(_context);
    }

    @Override
    public void update() {
        if (_dealingCard == null) {
            _dealingCard = _cards[_remainCardNumber - 1];
            --_remainCardNumber;
            Point beginPoint = _context._beginLocationOfPlayers[_currentPlayerIndex];
            _dealingCard.move(beginPoint.x, beginPoint.y);
              _cardOfPlayer[_currentPlayerIndex]++;
            _currentPlayerIndex = ((_currentPlayerIndex + 1) % 4);
          
        } else {
            if (!_dealingCard.isMoving()) {
                int cardOfPlayer = _cardOfPlayer[_currentPlayerIndex];
                // _players[_currentPlayerIndex][cardOfPlayer] = _dealingCard;
                if (_remainCardNumber > 0) {
                    _dealingCard = _cards[_remainCardNumber - 1];

                    --_remainCardNumber;
                    Point destPoint = new Point();

                    destPoint.x = _context._beginLocationOfPlayers[_currentPlayerIndex].x + _context._deltaVector[_currentPlayerIndex].x * cardOfPlayer;
                    destPoint.y = _context._beginLocationOfPlayers[_currentPlayerIndex].y + _context._deltaVector[_currentPlayerIndex].y * cardOfPlayer;
                    _dealingCard.move(destPoint.x, destPoint.y);
                    _cardOfPlayer[_currentPlayerIndex]++;
                    _currentPlayerIndex = ((_currentPlayerIndex + 1) % 4);
                } else {
                    if (_context._mainGame.getAdapter().isHavePassCardRequest()) {
                        _context._mainGame.getAdapter().finishDealCard();
                        _context.changeState(playStateType.PassingCardState);
                    }
                    if(_context._mainGame.getAdapter().passCardDone())
                    {
                        _context._mainGame.getAdapter().finishDealCard();
                        _context.changeState(playStateType.HandState);
                    }
                }
            }
            _dealingCard.updatePosition();
        }
    }

    @Override
    public playStateType getStateType() {
        return DealingState.playStateType.DealingState;
    }

    @Override
    public void render(Graphics graphic) {

        for (int index = _cards.length - 1; index >= 0; --index) {
            _cards[index].draw(graphic);
        }
//        _dealingCard.draw(graphic);

    }

    @Override
    public void Init() {
       
    }
}